package com.jintian.smart.platform.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.jintian.smart.base.api.system.form.*;
import com.jintian.smart.base.api.system.view.HelpCatalogVO;
import com.jintian.smart.base.api.system.view.HelpDetailVO;
import com.jintian.smart.base.api.system.view.HelpVO;
import com.jintian.smart.base.api.system.view.HelpViewRecordVO;
import com.jintian.smart.kernel.core.constant.SwaggerTagConst;
import com.jintian.smart.kernel.core.domain.ResponseDTO;
import com.jintian.smart.platform.system.service.HelpService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import com.jintian.smart.kernel.common.controller.SupportBaseController;
import com.jintian.smart.kernel.common.context.ContextHolder;
import com.jintian.smart.platform.system.service.HelpCatalogService;
import com.jintian.smart.platform.system.service.HelpUserService;
import com.jintian.smart.kernel.core.annotation.RepeatSubmit;
import org.beetl.sql.core.page.PageResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 帮助文档
 */
@Tag(name = SwaggerTagConst.Support.HELP_DOC)
@RestController
@RequestMapping("/sys/help")
public class HelpController extends SupportBaseController {
    @Resource
    private HelpCatalogService helpCatalogService;

    @Resource
    private HelpUserService helpUserService;

    @Resource
    private HelpService helpService;


    // --------------------- 帮助文档 【目录】 -------------------------

    @Operation(summary = "帮助文档目录-获取全部")
    @GetMapping("/catalog/getAll")
    public ResponseDTO<List<HelpCatalogVO>> getAll() {
        return ResponseDTO.ok(helpCatalogService.getAll());
    }

    // --------------------- 帮助文档 【用户】-------------------------

    @Operation(summary = "【用户】帮助文档-查看详情")
    @GetMapping("/user/view/{helpDocId}")
    @RepeatSubmit
    public ResponseDTO<HelpDetailVO> view(@PathVariable Long helpDocId, HttpServletRequest request) {
        return helpUserService.view(ContextHolder.getUser(), helpDocId);
    }

    @Operation(summary = "【用户】帮助文档-查询全部")
    @GetMapping("/user/queryAllHelpDocList")
    @RepeatSubmit
    public ResponseDTO<List<HelpVO>> queryAllHelpDocList() {
        return helpUserService.queryAllHelpDocList();
    }


    @Operation(summary = "【用户】帮助文档-查询 查看记录")
    @PostMapping("/user/queryViewRecord")
    @RepeatSubmit
    public ResponseDTO<PageResult<HelpViewRecordVO>> queryViewRecord(@RequestBody @Valid HelpViewRecordQueryForm helpViewRecordQueryForm) {
        return ResponseDTO.ok(helpUserService.queryViewRecord(helpViewRecordQueryForm));
    }

    // --------------------- 帮助文档 【目录管理】 -------------------------


    @Operation(summary = "帮助文档目录-添加")
    @PostMapping("/catalog/add")
    public ResponseDTO<String> addHelpDocCatalog(@RequestBody @Valid HelpCatalogAddForm helpCatalogAddForm) {
        return helpCatalogService.add(helpCatalogAddForm);
    }

    @Operation(summary = "帮助文档目录-更新")
    @PostMapping("/catalog/update")
    public ResponseDTO<String> updateHelpDocCatalog(@RequestBody @Valid HelpCatalogUpdateForm helpDocCatalogUpdateForm) {
        return helpCatalogService.update(helpDocCatalogUpdateForm);
    }

    @Operation(summary = "帮助文档目录-删除")
    @GetMapping("/catalog/delete/{helpDocCatalogId}")
    public ResponseDTO<String> deleteHelpDocCatalog(@PathVariable Long helpDocCatalogId) {
        return helpCatalogService.delete(helpDocCatalogId);
    }

    // --------------------- 帮助文档 【管理:增、删、查、改】-------------------------

    @Operation(summary = "【管理】帮助文档-分页查询")
    @PostMapping("/query")
    @SaCheckPermission("support:helpDoc:query")
    public ResponseDTO<PageResult<HelpVO>> query(@RequestBody @Valid HelpQueryForm queryForm) {
        return ResponseDTO.ok(helpService.query(queryForm));
    }

    @Operation(summary = "【管理】帮助文档-获取详情")
    @GetMapping("/getDetail/{helpDocId}")
    @SaCheckPermission("support:helpDoc:add")
    public ResponseDTO<HelpDetailVO> getDetail(@PathVariable Long helpDocId) {
        return ResponseDTO.ok(helpService.getDetail(helpDocId));
    }

    @Operation(summary = "【管理】帮助文档-添加")
    @PostMapping("/add")
    @RepeatSubmit
    public ResponseDTO<String> add(@RequestBody @Valid HelpAddForm addForm) {
        return helpService.add(addForm);
    }

    @Operation(summary = "【管理】帮助文档-更新")
    @PostMapping("/update")
    @RepeatSubmit
    public ResponseDTO<String> update(@RequestBody @Valid HelpUpdateForm updateForm) {
        return helpService.update(updateForm);
    }

    @Operation(summary = "【管理】帮助文档-删除")
    @GetMapping("/delete/{helpDocId}")
    public ResponseDTO<String> delete(@PathVariable Long helpDocId) {
        return helpService.delete(helpDocId);
    }

    @Operation(summary = "【管理】帮助文档-根据关联id查询")
    @GetMapping("/queryHelpDocByRelationId/{relationId}")
    public ResponseDTO<List<HelpVO>> queryHelpDocByRelationId(@PathVariable Long relationId) {
        return ResponseDTO.ok(helpService.queryHelpDocByRelationId(relationId));
    }
}